{{extend 'layout.html'}}

{{include 'default/err_check.html'}}

<div class="breadcrumbs">
    <a href="{{=URL(r=request, c='default', f='index')}}" title="Home">Home</a>
    / <a href="{{=URL(r=request, c='default', f='forum', args=[forum.id])}}"
       title="Back To {{=forum.forum_title}}">{{=forum.forum_title}}</a>
    / <b>{{=XML(topic.title)}}</b>
    {{if view_info['subscribed_to_topic']:}}<i>(You are subscribed to this topic)</i>{{pass}}
</div>

<div class="subnav">
    <a href="{{=URL(r=request, c='default', f='forum', args=[forum.id])}}"
        title="Back To {{=forum.forum_title}}">Back</a>
    {{if (security_info['can_reply'] and not topic.locked_flag) or auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
        |
        <a href="#add_comment" title="Add a Topic Response">Add a Topic Response</a>
    {{pass}}
    {{if auth.is_logged_in():}}
        |
        {{if view_info['subscribed_to_topic']:}}
            <a href="{{=URL(r=request, c='default', f='topic', args=[topic.id, 0])}}" title="Unsubscribe from Topic">Unsubscribe from Topic</a>
        {{else:}}
            <a href="{{=URL(r=request, c='default', f='topic', args=[topic.id, 1])}}" title="Subscribe to Topic">Subscribe to Topic</a>
        {{pass}}
    {{pass}}
</div>

<div>{{=XML(view_info['pagination_widget'])}}</div>

<form id="topics_form" method="post" name="topics_form" action="">
    <fieldset id="topics_form_fields">
        <input type="hidden" id="topic_id" name="topic_id" value="{{=topic.id}}" />
        <input type="hidden" id="forum_id" name="forum_id" value="{{=forum.id}}" />
        <input type="hidden" id="form_submitted" name="form_submitted" value="1" />
        <input type="hidden" id="action" name="action" value="remove" />
        <input type="hidden" id="confirm_delete_msg" name="confirm_delete_msg" value="Please confirm the removal of the selected topics" />
        <input type="hidden" id="warn_delete_parent_msg" name="warn_delete_parent_msg" value="Warning: Removing this topic will remove all its sibling topics" />
        <input type="hidden" id="confirm_inapp" name="confirm_inapp" value="Please confirm you wish to report this topic as inappropriate" />
        <input type="hidden" id="c" name="c" value="{{=view_info['anon_captcha_base64']}}" />

        {{if view_info.has_key('preview'):}}
            <div class="preview">
                <b>Message Preview</b>
                <div class="hr"><hr /></div>
                <div class="bottom-separator">{{=XML(forumhelper.html_normalize(request.vars.get('response_content', '')), sanitize=True)}}</div>
                <a href="#add_comment" title="Add This Comment">Add This Comment</a>
            </div>
        {{pass}}

        {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
            <div class="bottom-separator">
                <input type="submit" id="submit_del" name="remove" value="Remove Checked Topics" onclick="return confirm(document.getElementById('confirm_delete_msg').value);" />
            </div>
        {{pass}}

        <table class="bordered" style="width:100%;">
            <thead>
                <tr>
                    <th colspan="2"><b>{{=XML(topic.title, sanitize=True)}}</b></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td style="width:15%;">
                        <div class="topic-information">
                            By:
                            {{if not topic.creation_user_id:}}
                                <b class="small">Anonymous User</b>
                            {{else:}}
                                {{if auth.is_logged_in():}}
                                    <a href="{{=URL(r=request, c='pm', f='message_new', args=[topic.creation_user_id])}}"
                                       title=""
                                       class="smallb">{{=forumhelper.get_display_name(topic.creation_user_id)}}</a>
                                {{else:}}
                                    <b>{{=forumhelper.get_display_name(topic.creation_user_id)}}</b>
                                {{pass}}
                                {{if forumhelper.get_system_property('zfsp_use_ranking_system', '') != '':}}
                                    <br />Rank: {{=forumhelper.get_user_rank(topic.creation_user_id)}}
                                {{pass}}
                                <br />Postings: {{=forumhelper.get_member_property('zfmp_postings', topic.creation_user_id, '0')}}
                                <br />From: {{=XML(forumhelper.get_member_property('zfmp_country', topic.creation_user_id, '<i>Unknown</i>'))}}
                                {{if forumhelper.get_system_property('zfsp_allow_member_avatars', '') != ''\
                                     and forumhelper.has_member_avatar(topic.creation_user_id):}}
                                    <img src="{{=URL(r=request, c='admin', f='get_avatar_image', args=[topic.creation_user_id])}}" alt="" />
                                {{pass}}
                            {{pass}}
                        </div>
                    </td>
                    <td>
                        <div style="float:left;" class="topic-information">
                            <a id="{{=topic.id}}">Posted</a>:
                            {{=topic.creation_date.strftime('%a, %d %B, %Y - %I:%M %p')}}
                        </div>
                        <div style="float:right;" class="topic-information">
                            {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                <a href="{{=URL(r=request, c='default', f='edit_topic', args=[topic.id])}}"
                                   title="Edit Topic">Edit Topic</a> |
                            {{pass}}
                            {{if (security_info['can_reply'] and not topic.locked_flag) or auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                <a href="#add_comment"
                                   title=""
                                   onclick="reply_quote('{{=forumhelper.get_display_name(topic.creation_user_id)}}', 'topic_id_{{=topic.id}}');">Reply w/Quote</a> |
                            {{pass}}
                            <a href="{{=URL(r=request, c='default', f='report_inappropriate', args=[topic.id, topic.id])}}"
                               onclick="return confirm(document.getElementById('confirm_inapp').value);"
                               title="Report as inappropriate">Report as inappropriate</a>
                            {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                |
                                <input type="checkbox" id="remove_topic_parent_{{=topic.id}}"
                                       name="remove_topic_parent_{{=topic.id}}"
                                       value="{{=topic.id}}"
                                       onclick="if(this.checked){alert(document.getElementById('warn_delete_parent_msg').value);}"
                                       style="vertical-align:middle;" />
                                <label for="remove_topic_parent_{{=topic.id}}" style="font-weight:bold;">Remove Topic</label>
                            {{pass}}
                        </div>
                        <div class="hr" style="clear:both;"><hr /></div>

                        <!-- Contents of the PARENT topic: //-->
                        <div id="topic_id_{{=topic.id}}" class="both-separator">{{=XML(topic.contents, sanitize=True)}}</div>
                    </td>
                </tr>
                {{if len(children):}}
                    {{for row, this_child in enumerate(children):}}
                        <tr>

                            <td style="width:15%;">
                                <div class="topic-information">
                                    By:
                                    {{if not this_child.creation_user_id:}}
                                        <b class="small">Anonymous User</b>
                                    {{else:}}
                                        {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                            <a href="{{=URL(r=request, c='pm', f='message_new', args=[this_child.creation_user_id])}}"
                                               title="">{{=forumhelper.get_display_name(this_child.creation_user_id)}}</a>
                                        {{else:}}
                                            <b>{{=forumhelper.get_display_name(this_child.creation_user_id)}}</b>
                                        {{pass}}
                                        {{if forumhelper.get_system_property('zfsp_use_ranking_system', '') != '':}}
                                            <br />Rank: {{=forumhelper.get_user_rank(this_child.creation_user_id)}}
                                        {{pass}}
                                        <br />Postings: {{=forumhelper.get_member_property('zfmp_postings', this_child.creation_user_id, '0')}}
                                        <br />From: {{=XML(forumhelper.get_member_property('zfmp_country', this_child.creation_user_id, '<i>Unknown</i>'))}}
                                        {{if forumhelper.get_system_property('zfsp_allow_member_avatars', '') != ''\
                                             and forumhelper.has_member_avatar(this_child.creation_user_id):}}
                                            <img src="{{=URL(r=request, c='admin', f='get_avatar_image', args=[this_child.creation_user_id])}}" alt="" />
                                        {{pass}}
                                    {{pass}}
                                </div>
                            </td>
                            <td>
                                <div style="float:left;" class="topic-information">
                                    <a id="{{=this_child.id}}">Posted</a>:
                                    {{=this_child.creation_date.strftime('%a, %d %B, %Y - %I:%M %p')}}
                                </div>
                                <div style="float:right;" class="topic-information">
                                    {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                        <a href="{{=URL(r=request, c='default', f='edit_topic', args=[this_child.id])}}" title="Edit Topic">Edit Topic</a> |
                                    {{pass}}
                                    {{if (security_info['can_reply'] and not topic.locked_flag) or auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                        <a href="#add_comment"
                                           title=""
                                           onclick="reply_quote('{{=forumhelper.get_display_name(this_child.creation_user_id)}}', 'topic_id_{{=this_child.id}}');">Reply w/Quote</a> |
                                    {{pass}}
                                    <a href="{{=URL(r=request, c='default', f='report_inappropriate', args=[this_child.id, topic.id])}}"
                                       onclick="return confirm(document.getElementById('confirm_inapp').value);"
                                       title="Report as inappropriate">Report as inappropriate</a>
                                    {{if auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                                        |
                                        <input type="checkbox" id="remove_topic_child_{{=this_child.id}}"
                                               name="remove_topic_child_{{=this_child.id}}"
                                               value="{{=this_child.id}}"
                                               style="vertical-align:middle;" />
                                        <label for="remove_topic_child_{{=this_child.id}}"
                                               style="font-weight:bold;">Remove Topic</label>
                                    {{pass}}
                                </div>
                                <div class="hr" style="clear:both;"><hr /></div>

                                <!-- Contents of the CHILDREN TOPICS //-->
                                <div id="topic_id_{{=this_child.id}}" class="both-separator">{{=XML(forumhelper.html_normalize(this_child.contents), sanitize=True)}}</div>
                            </td>
                        </tr>
                    {{pass}}
                {{pass}}
            </tbody>
        </table>

        {{if not len(children):}}
            <p class="information" style="font-style:italic;text-align:center;">This topic contains no responses.</p>
        {{pass}}

        {{if topic.locked_flag:}}
            <p class="information" style="font-style:italic;text-align:center;">The forum administrator has restricted adding any more comments to this topic.</p>
        {{pass}}

        {{if (security_info['can_reply'] and not topic.locked_flag) or auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}

            <div class="both-separator">
                <a id="add_comment" name="add_comment">&nbsp;</a>

                <table class="bordered" style="margin-left:auto;margin-right:auto;width:90%;">
                    <thead>
                        <tr>
                            <th colspan="2">Add Reply</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="width: 25%;">Title:</td>
                            <td style="width: 75%;"><b>{{=XML(topic.title, sanitize=True)}}</b></td>
                        </tr>
                        <tr>
                            <td>By:</td>
                            <td>{{=forumhelper.get_display_name(None, 'Anonymous User')}}, on {{=request.now.strftime('%A, %d %B, %Y')}}</td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle;">Contents:</td>
                            <td>
                                <textarea name="response_content"
                                          id="z_content"
                                          style="width: 95%;height: 200px;">{{=request.vars.get('response_content', '')}}</textarea>
                                <br />You may use a subset of HTML tags such as &lt;i&gt;, &lt;b&gt;, &lt;a href..&gt;, &lt;strong&gt;, &lt;code&gt;, etc.
                                Not all HTML tags will be accepted.
                            </td>
                        </tr>
                        {{if not auth.has_membership(forum_memberships['FORUM_ADMINISTRATOR']):}}
                            <tr>
                                <td><span class="required">*</span>
                                    Verify your humanity:
                                </td>
                                <td>
                                    <input type="text" id="captcha_response" name="captcha_response" value="" style="width:100px;" />
                                    <b>{{=view_info['anon_captcha']}}</b>
                                    <span class="warning">(Please Enter the acces code shown in order to post your message.)</span>
                                </td>
                            </tr>
                        {{pass}}
                        <tr>
                            <td style="text-align:right;" colspan="2">
                                <input type="submit" id="preview" name="preview" value="Preview Topic Response" />
                                <input type="submit" id="add" name="add" value="Add Topic Response" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        {{pass}}

    </fieldset>
</form>

<script>
<!--
    function quote(blockquote, topic_id) {
        var blockquote;
        var response_form = document.forms['response_form'];
        var current_content = response_form['content'];
        var new_content = current_content.value + blockquote;
        var reply_to_topic_id = document.getElementById('reply_to_topic_id');
        reply_to_topic_id.value = topic_id;
        current_content.value = new_content;
        current_content.focus();
        return false;
    }

    function toggle_thread(element_id) {
        var displaystyle = document.getElementById(element_id).style;
        if (displaystyle.display == 'none') {
            displaystyle.display = 'block';
        } else {
            displaystyle.display = 'none';
        }
        return false;
    }

    function reply_quote(username, topic_id_text) {
        var topic_text = document.getElementById(topic_id_text).innerHTML;
        topic_text = topic_text.replace(/<br>/g, '\n');
        var quote_text = '\n\n[quote][smallb]From: ' + username + '[/smallb]\n\n' + topic_text + '\n[/quote]';
        document.getElementById('z_content').value = quote_text;
        document.getElementById('z_content').focus();
    }

    PyforumJS.Controller.initTextareaEditor();

    //PyforumJS.Controller.lineIze();
//-->
</script>
